package com.axapoint.opoc.core.dao;

import java.util.Collection;
import java.util.LinkedList;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import com.anasoft.os.daofusion.bitemporal.WrappedBitemporalProperty;
import com.anasoft.os.daofusion.bitemporal.WrappedValueAccessor;

@Entity
@Table(name = "OrganizationChannelType")
public class OrganizationChannelType extends ObjectRegistration<OrganizationChannelType, OrganizationChannelType.Value, OrganizationChannelType.Bitemporal> {
	private static final long serialVersionUID = 9168710745762860711L;

	@Entity
	@Table(name = "OrganizationChannelTypeValues")
	public static final class Value extends _Value<OrganizationChannelType, Bitemporal> {
		private static final long serialVersionUID = 1L;
		private String name;

		protected Value() {};
		Value(OrganizationChannelType oct, String systemId, String userId, Operation operation, String comment, Value value) {
			super(oct, systemId, userId, operation, comment);
			this.name = value.name;			
		}
		public Value(String name) {
			this.name = name;
		}
		public String getName() {
			return name;
		}
		protected void setName(String name) {
			this.name = name;
		}
		@Override
		public String diff(_Value<OrganizationChannelType, Bitemporal> otherValue, DateTime t0, DateTime t1) {
			StringBuffer sb = new StringBuffer();
			Value other = (Value)otherValue;
			
			if (name != null && !name.equals(other.name)) {
				sb.append("OrganizationChannelType.name: ").append(name).append(" - ").append(other.name).append('\n');
			}
			
			return sb.toString();
		}
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append(super.toString());
			sb.append("  name=").append(name).append('\n');

			int cnt = 0;
			for (OrganizationChannel.Value v : getEntity().getOrganizationChannels()) {
				if (v.getBitemporal().getValidityInterval().contains(getBitemporal().getValidityInterval().getStart())) {
					cnt++;
				}
			}
			sb.append("  #organizationChannels=").append(cnt).append('\n');			
			
			return sb.toString();
		}
	}
	
	@Entity
	@Table(name = "OrganizationChannelTypeBitemporal")
	public static final class Bitemporal extends _Bitemporal<Bitemporal, Value> {
		private static final long serialVersionUID = -2132187534862370208L;
		
		protected Bitemporal() {}
	    private Bitemporal(OrganizationChannelType.Value value, Interval validityInterval) {
	        super(value, validityInterval);
	    }
	    
	    @Override
	    public Bitemporal copyWith(Interval validityInterval) {
	    	return new Bitemporal(value, validityInterval);
	    }
	}

	@SuppressWarnings("deprecation")
	@OneToMany(fetch = FetchType.LAZY)
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name="organizationChannelTypeId")
    private Collection<OrganizationChannel.Value> organizationChannels = new LinkedList<OrganizationChannel.Value>();	

	public OrganizationChannelType() {
		super();
	}
	public Collection<OrganizationChannel.Value> getOrganizationChannels() {
		return organizationChannels;
	}
	protected void setOrganizationChannels(Collection<OrganizationChannel.Value> organizationChannels) {
		this.organizationChannels = organizationChannels;
	}
    public WrappedBitemporalProperty<Value, Bitemporal> getValues() {
    	return new WrappedBitemporalProperty<Value, Bitemporal>(values, new WrappedValueAccessor<Value,Bitemporal>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Bitemporal wrapValue(Value value, Interval validityInterval) {
				Bitemporal bi = new Bitemporal(value, validityInterval);
				return bi;
			}
    	});
    }
}
